﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace GameStudio.IDE.Store
{
    public class ExecuteProvider
    {
        private readonly string _xmlPath;

        public ExecuteProvider(string xmlPath)
        {
            _xmlPath = xmlPath;
        }
        
        public IEnumerable<ItemBase> GetItems(string key)
        {
            XDocument doc = XDocument.Load(_xmlPath);
            IEnumerable<ItemBase> items = (from element in doc.Elements()
                                           select
                                               (element.HasElements == true)
                                                   ? (ItemBase) new ValueItem {Key = key, Value = element.Value}
                                                   : new WindowItem {Key = key}).OfType<ItemBase>();
            return items;
        }
        public void UpdateItem(string key, object value)
        {
            XDocument doc = XDocument.Load(_xmlPath);
            doc.Element(key).SetValue(value);
            //doc.Save()
        }
        public void InsertItem(string key, ItemBase newItem)
        {
            XDocument doc = XDocument.Load(_xmlPath);
        }
        public void DeleteItem(string key)
        {
            XDocument doc = XDocument.Load(_xmlPath);
            doc.Element(key).Elements().Remove(); //delete the elements inside this element -> the window
            doc.Element(key).Remove();// remove the element only -> value item
        }
    }
}
